import cTitle from "components/title";
import AMap from "AMap";
export default {
  components: {
    cTitle
  },
  data() {
    return {
      goodsId: "",
      shipAreaType: "",
      city: "",
      longitude: "",
      latitude: "",
      distance: 0,
      shopList: [],
      geoJSON: [],
      centerPath: [],
      shipAreas: []
    };
  },
  activated() {
    this.goodsId = Number(this.$route.query.goodsId);
    this.city = this.$route.query.city;
  },
  created() {},
  mounted() {
    this.getRule();
  },
  methods: {
    setshopMap() {
      var centerPath = Array.from(this.centerPath);
      var map = new AMap.Map("shopMap", {
        center: centerPath,
        zoom: 14
      });
      var marker = new AMap.Marker({
        icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
        position: centerPath
      });
      marker.setMap(map);
      // 构造矢量圆形
      console.log(this.distance);
      var circle = new AMap.Circle({
        center: new AMap.LngLat(centerPath[0], centerPath[1]), // 圆心位置
        radius: this.distance, //半径
        strokeColor: "#F33", //线颜色
        strokeOpacity: 1, //线透明度
        strokeWeight: 3, //线粗细度
        fillColor: "#ee2200", //填充颜色
        fillOpacity: 0.35 //填充透明度
      });
      map.add(circle);
      // 缩放地图到合适的视野级别
      map.setFitView([circle]);
    },
    setDeliveryMap() {
      var map = new AMap.Map("deliveryMap", {
        center: [this.longitude, this.latitude],
        zoom: 10
      });
      // 创建 geoJSON 实例
      var geoJson = new AMap.GeoJSON({
        geoJSON: Array.from(this.geoJSON), // GeoJSON对象
        getPolygon: function(geojson, lnglats) {
          //还可以自定义getMarker和getPolyline
          return new AMap.Polygon({
            path: lnglats,
            fillOpacity: 0.35,
            strokeColor: "white",
            fillColor: "#ee2200"
          });
        }
      });
      map.add(geoJson);
    },
    getRule() {
      let query = {
        city: this.$route.query.city,
        goods_id: this.$route.query.goodsId
      };
      $http.get("plugin.yz-supply-cake.frontend.index.index.getRule", query, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            console.log(res.code);
            if (res.code !== 200) {
              Toast(res.data.msg);
              this.description = res.data.msg;
              return;
            }
            let data = res.data;
            this.description = data?.description;
            this.shipAreaType = data?.ship_area_type;
            let shopList = data?.shop || [];
            if (this.shipAreaType === "1" || this.shipAreaType === 1) {
              //地图区域)
              let shipAreas = JSON.parse(data?.ship_areas) || {};
              let areas = shipAreas.areas || [];
              this.geoJSON = this.ForDataChange(areas); //处理数据格式
              this.shipAreas = shipAreas;
              this.latitude = areas[0]?.points[0]?.lat;
              this.longitude = areas[0]?.points[0]?.lng;
              this.setDeliveryMap();
            }
            if (this.shipAreaType === "2" || this.shipAreaType === 2) {
              //门店半径区域)
              let shipRule = JSON.parse(data?.ship_rule) || {};
              let nonDefault = shipRule.non_default || {};
              let distance = "";
              for (let key in nonDefault) {
                distance = nonDefault[key].distance;
              }
              this.distance = distance * 1000;
              this.shopList = shopList;
              console.log(this.shopList);
              this.centerPath[0] = shopList[0]?.lng;
              this.centerPath[1] = shopList[0]?.lat;
              if (this.shopList.length > 0) {
                //没有门店的时候就是全国配送，会导致报错
                this.setshopMap();
              }
            }
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    ForDataChange(areas) {
      let GeoJSON = [];
      let pointsArr = [];
      areas.map((item, index) => {
        let points = item.points;
        pointsArr.push([]);
        for (let i in points) {
          if (points[i].lng) {
            pointsArr[index].push([points[i].lng, points[i].lat]);
          }
        }
        GeoJSON.push({
          type: "Feature",
          //这里你需要什么属性都可以在此写入
          properties: {
            name: "",
            risk: "",
            landType: "",
            landId: ""
            //……
          },
          geometry: {
            type: "Polygon",
            coordinates: [pointsArr[index]]
          }
        });
      });
      return GeoJSON;
    }
    //将数据处理成GeoJSON
  }
};
